<?php

App::uses('AppController', 'Controller');

/**
 * Users Controller
 *
 * @property User $User
 */
class UsersController extends AppController {

    public function isAuthorized($user) {
        if (parent::isAuthorized($user) && $user['role'] === 'admin')
            return true;

        if (isset($user['role']) && $user['role'] === 'socio') {
            if (in_array($this->action, array('edit', 'changePass', 'loadEditor'))) {
                return true; //Admin can access every action
            }
        }

        return false;
    }

//  extending the parent method
    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('add, index'); // Letting users register themselves
    }

    public function login() {
        $this->set('title_for_layout', 'Iniciar Sesion');
        $this->Session->destroy();
        if ($this->request->is('post')) {
            if ($this->Auth->login()) {

                if ($this->logUser($this->Session->read('Auth.User.id'), $this->Session->read('Auth.User.username'), $this->Session->read('Auth.User.role'))) {
                    $this->redirect($this->Auth->redirect());
                }
            } else {
                $this->Session->setFlash(__('Usuario o clave incorrectos, intente nuevamente.'));
            }
        }
        $this->layout = 'login';
    }

    public function logUser($id = null, $username = null, $role) {
        $this->loadModel('LogsUser');
        $data = array(
            'LogsUser' => array(
                'user_id' => $id,
                'username' => $username,
                'role' => $role,
                'date' => date("Y-m-d: H:i:s", time()))
        );
        $this->LogsUser->create();
        if ($this->LogsUser->save($data)) {
            return true;
        }
    }

    public function logout() {
        $this->redirect($this->Auth->logout());
    }

    /**
     * index method
     *
     * @return void
     */
    public function index() {
        $this->User->recursive = 0;

        $this->set('users', $this->paginate());
    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        if (!$this->User->exists($id)) {
            throw new NotFoundException(__('Invalid user'));
        }
        $options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
        $this->set('user', $this->User->find('first', $options));
    }

    /**
     * add method
     *
     * @return void
     */
    public function add() {
        if ($this->request->is('post')) {
            $this->User->create();
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('The user has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
            }
        }
        $shops = $this->User->Shop->find('list');
        $this->set(compact('shops'));
    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function edit($id = null) {
        if (!$this->User->exists($id)) {
            throw new NotFoundException(__('Invalid user'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('The user has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
            }
        } else {
            $options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
            $this->request->data = $this->User->find('first', $options);
        }
        $shops = $this->User->Shop->find('list');
        $this->set(compact('shops'));
    }

    /**
     * delete method
     *
     * @throws NotFoundException
     * @throws MethodNotAllowedException
     * @param string $id
     * @return void
     */
    public function delete($id = null) {
        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Invalid user'));
        }
        $this->request->onlyAllow('post', 'delete');
        if ($this->User->delete()) {
            $this->Session->setFlash(__('User deleted'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('User was not deleted'));
        $this->redirect(array('action' => 'index'));
    }

}
